﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Imagin.Controls.Common"
    xmlns:Controls.Common.Extensions="clr-namespace:Imagin.Controls.Common.Extensions"
    xmlns:Common.Data.Converters="clr-namespace:Imagin.Common.Data.Converters;assembly=Imagin.Common">
    <Common.Data.Converters:NullToBooleanConverter x:Key="NullToBooleanConverter"/>

    <Style TargetType="local:TreeViewComboBox">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:TreeViewComboBox}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="15"/>
                        </Grid.ColumnDefinitions>
                        <ToggleButton 
                            Grid.ColumnSpan="2" 
                            x:Name="ToggleButton" 
                            Foreground="{TemplateBinding Foreground}"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}"
                            Focusable="false" 
                            IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
                            ClickMode="Press">
                            <ToggleButton.Template>
                                <ControlTemplate TargetType="{x:Type ToggleButton}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition />
                                            <ColumnDefinition Width="20" />
                                        </Grid.ColumnDefinitions>
                                        <Border 
                                            x:Name="Border" 
                                            Grid.ColumnSpan="2" 
                                            Background="{TemplateBinding Background}" 
                                            BorderBrush="{TemplateBinding BorderBrush}"
                                            BorderThickness="{TemplateBinding BorderThickness}"/>
                                        <Path 
                                            x:Name="PART_Arrow" 
                                            Grid.Column="1" 
                                            Fill="{TemplateBinding Foreground}" 
                                            HorizontalAlignment="Center" 
                                            VerticalAlignment="Center" 
                                            Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
                                            RenderTransformOrigin="0.5,0.5">
                                            <Path.RenderTransform>
                                                <RotateTransform/>
                                            </Path.RenderTransform>
                                        </Path>
                                    </Grid>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsChecked" Value="True">
                                            <Trigger.EnterActions>
                                                <BeginStoryboard>
                                                    <Storyboard>
                                                        <DoubleAnimation Storyboard.TargetName="PART_Arrow" Storyboard.TargetProperty="(Path.RenderTransform).(RotateTransform.Angle)" To="90" Duration="0:0:0.4"/>
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </Trigger.EnterActions>
                                            <Trigger.ExitActions>
                                                <BeginStoryboard>
                                                    <Storyboard>
                                                        <DoubleAnimation Storyboard.TargetName="PART_Arrow" Storyboard.TargetProperty="(Path.RenderTransform).(RotateTransform.Angle)" To="0" Duration="0:0:0.4"/>
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </Trigger.ExitActions>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </ToggleButton.Template>
                        </ToggleButton>
                        <ContentControl 
                            x:Name="PART_Content"
                            IsHitTestVisible="False"  
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            Margin="{TemplateBinding Padding}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <ContentControl.Resources>
                                <DataTemplate x:Key="DefaultContentTemplate">
                                    <TextBlock Text="{Binding Header}"/>
                                </DataTemplate>
                            </ContentControl.Resources>
                            <ContentControl.Style>
                                <Style TargetType="ContentControl">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding ContentTemplate, RelativeSource={RelativeSource AncestorType={x:Type local:TreeViewComboBox}}, Converter={StaticResource NullToBooleanConverter}}" Value="True">
                                            <Setter Property="ContentTemplate" Value="{Binding ContentTemplate, RelativeSource={RelativeSource AncestorType={x:Type local:TreeViewComboBox}}}"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding ContentTemplate, RelativeSource={RelativeSource AncestorType={x:Type local:TreeViewComboBox}}, Converter={StaticResource NullToBooleanConverter}}" Value="False">
                                            <Setter Property="ContentTemplate" Value="{StaticResource DefaultContentTemplate}"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ContentControl.Style>
                        </ContentControl>
                        <Popup 
                            x:Name="PART_Popup" 
                            AllowsTransparency="True" 
                            Focusable="False" 
                            IsOpen="{TemplateBinding IsDropDownOpen}" 
                            Placement="Bottom" 
                            PopupAnimation="Fade"
                            StaysOpen="True">
                            <Grid 
                                x:Name="DropDown" 
                                SnapsToDevicePixels="True"
                                MinWidth="{TemplateBinding ActualWidth}">
                                <Border 
                                    x:Name="DropDownBorder"
                                    Background="{DynamicResource Brush.ComboBox.DropDown.Background}" 
                                    BorderThickness="1" 
                                    BorderBrush="{DynamicResource Brush.ComboBox.DropDown.Border}"
                                    Padding="5">
                                    <local:TreeViewExt 
                                        x:Name="PART_TreeView" 
                                        BorderThickness="0" 
                                        ItemBindingGroup="{TemplateBinding ItemBindingGroup}"
                                        ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                                        ItemContainerStyleSelector="{TemplateBinding ItemContainerStyleSelector}"
                                        ItemsPanel="{TemplateBinding ItemsPanel}"
                                        ItemsSource="{TemplateBinding ItemsSource}"
                                        ItemStringFormat="{TemplateBinding ItemStringFormat}"
                                        ItemTemplate="{TemplateBinding ItemTemplate}"
                                        ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                        SelectedObject="{Binding SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}"/>
                                </Border>
                            </Grid>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasItems" Value="False">
                            <Setter TargetName="DropDownBorder" Property="MinHeight" Value="125" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>